<?php
session_start();
//包含头
include '../PHP/head.php';
?>
<?php
//包含bootstrap的顶端按钮
include '../PHP/menu.php';
?>


<script type="text/javascript">
//需要一个异步删除记录的函数

	$(document).ready(function() {
    $('button.deletedata').on('click', function() {
					var requestData = {code: $(this).attr("id"), key: "id", table: "task_list"};
                    $('#notes').load('del_data.php', requestData);
                    $(this).parent().parent().hide();
					});

			});
</script>

<br />
<br />
<p class="text-center lead">模块报价项目安排</p>
<?php
if(isset($_GET['project_id'])) {
	//查询编号信息
	require_once ('../FNS/db_fns.php');
	
	$project_id = $_GET['project_id'];
	$subject = $_GET['subject'];
	$position = strtoupper($subject);
	
	//数据过滤和处理

	if (! get_magic_quotes_gpc()) {
	$project_id=addslashes ($project_id);
	}
	
	$db = db_connect();
	$query="select projects.code as project_code, projects.name as project_name, projects.sale as sale_id, projects.entrust_date, users.name as sale_name from projects inner join users on projects.sale = users.id where projects.id = '".$project_id."'";
	
	$result=$db->query ($query);
	$num_result=$result->num_rows;
	
	//拿出数据
	$row=$result->fetch_assoc ();
	$project_code = stripslashes ($row['project_code']);
	$project_name = stripslashes ($row['project_name']);
	$sale_name = stripslashes ($row['sale_name']);
	$sale_id = stripslashes ($row['sale_id']);

	
	$result->free();
	
	//列出编号基本信息
	echo "
		<br />
		<div class='row'>
			<div class='col-md-2'><p class='text-right'>报价编号：</p></div>
			<div class='col-md-2'><p>".$project_code."</p></div>
			<div class='col-md-2'><p class='text-right'>项目名称：</p></div>
			<div class='col-md-4'><p>".$project_name."</p></div>
			<div class='col-md-1'><p class='text-right'>销售：</p></div>
			<div class='col-md-1'><p>".$sale_name."</p></div>
		</div>
		<br />
	
		<br />
		<form class='form-inline' action = '../PHP/plan_task.php' method = 'post'>
		<input type='hidden' name = 'project_id' value = '".$project_id."'>
		<input type='hidden' name = 'subject' value = '".$subject."'>
		<div class='form-group'>
		<label for='engineer'>工程师：</label>
		<select class='form-control' name='engineer'>
	";
		


	//把专业工程师搜出来
	$query="select * from users where position LIKE '%".$position."%'";
	$result=$db->query ($query);
	$num_result=$result->num_rows;
	
	//循环输出数据
	
	for ($i=0; $i < $num_result; $i++) {

	$row=$result->fetch_assoc ();
	
	$user_id = stripslashes ($row['id']);
	$user_name = stripslashes ($row['name']);
	
	echo "<option value = '".$user_id."'>".$user_name."</option>";
	
	}
	$result->free();
	
	echo "
		</select>
	</div>
	<button type='submit' class='btn btn-success'>增加</button>
	</form>
	<div id='notes'>
		<p>操作信息栏</p>
	</div>
	
	";

}

if (isset($_POST['engineer'])) {
	
	require_once ('../FNS/db_fns.php');
	
	$project_id = trim($_POST['project_id']);
	$subject = trim($_POST['subject']);
	$engineer = trim($_POST['engineer']);
	$position = strtoupper($subject);
		
	//数据过滤和处理
	//加引号避免输入数据库出错
	if (! get_magic_quotes_gpc()) {	
	$project_id = addslashes ($project_id);
	$subject = addslashes ($subject);
	$engineer = addslashes ($engineer);
	
	}
	
	$plan_date = date('Y-m-d');
	
	//复现上半部分表单
	
	$db = db_connect();
	$query="select projects.code as project_code, projects.name as project_name, projects.sale as sale_id, projects.entrust_date, users.name as sale_name from projects inner join users on projects.sale = users.id where projects.id = '".$project_id."'";
	
	$result=$db->query ($query);
	$num_result=$result->num_rows;
	
	//拿出数据
	$row=$result->fetch_assoc ();
	$project_code = stripslashes ($row['project_code']);
	$project_name = stripslashes ($row['project_name']);
	$sale_name = stripslashes ($row['sale_name']);
	$sale_id = stripslashes ($row['sale_id']);

	
	$result->free();
	
	//列出编号基本信息
	echo "
		<br />
		<div class='row'>
			<div class='col-md-2'><p class='text-right'>报价编号：</p></div>
			<div class='col-md-2'><p>".$project_code."</p></div>
			<div class='col-md-2'><p class='text-right'>项目名称：</p></div>
			<div class='col-md-4'><p>".$project_name."</p></div>
			<div class='col-md-1'><p class='text-right'>销售：</p></div>
			<div class='col-md-1'><p>".$sale_name."</p></div>
		</div>
		<br />
	
		<br />
		<form class='form-inline' action = '../PHP/plan_task.php' method = 'post'>
		<input type='hidden' name = 'project_id' value = '".$project_id."'>
		<input type='hidden' name = 'subject' value = '".$subject."'>
		<div class='form-group'>
		<label for='engineer'>工程师：</label>
		<select class='form-control' name='engineer'>
	";
		


	//把专业工程师搜出来
	$query="select * from users where position LIKE '%".$position."%'";
	$result=$db->query ($query);
	$num_result=$result->num_rows;
	
	//循环输出数据
	
	for ($i=0; $i < $num_result; $i++) {

	$row=$result->fetch_assoc ();
	
	$user_id = stripslashes ($row['id']);
	$user_name = stripslashes ($row['name']);
	
	echo "<option value = '".$user_id."'>".$user_name."</option>";
	
	}
	$result->free();
	
	echo "
		</select>
	</div>
	<button type='submit' class='btn btn-success'>增加</button>
	</form>
	<div id='notes'>
		<p>操作信息栏</p>
	</div>
	
	";
	
	
	//插入数据
	//连接数据库并插入数据
	//没有判断是否成功，原意图为：如果接下来的列表更新了，说明成功了。

	$query="insert into task_list (`project_id`, `user_id`, `subject`, `plan_date`, `status`) value ('".$project_id."', '".$engineer."', '".$subject."', '".$plan_date."', 'NN')";
	$result=$db->query ($query);
	
	}


?>

<br />
<br />
<p class="text-center lead">安排列表：</p>

<?php
//引用必要的函数
require_once ('../FNS/db_fns.php');


//连接数据库并搜索数据

$db= db_connect();

$query="select task_list.id as task_id, users.name as user_name from task_list inner join users on task_list.user_id = users.id where subject = '".$subject."' and project_id = '".$project_id."'";
$result=$db->query ($query);

//获取记录数量
$num_results=$result->num_rows;


//写表头
echo "<table class='table table-striped'>";
echo "<tr>";
echo "<td align='center'><strong>已安排人员</strong></td>";
echo "<td align='center'>操作</td>";
echo "</tr>";



//循环编制表格
for ($i=0; $i < $num_results; $i++) {

	$row=$result->fetch_assoc ();
	
	$task_id = stripslashes ($row['task_id']);
	$user_name = stripslashes ($row['user_name']);
	
	echo "<tr>";
    echo "<td align ='center'>".$user_name."</td>";
	echo "<td align='center'><button class='btn btn-default deletedata' id='".$task_id."'>删除</button></td>";
	echo "</tr>";
	
}
echo "</table>";

$result->free();
$db->close();

?>

</div>
</body>
</html>
